package com.yanfan.iot.service.impl;

import com.yanfan.iot.cache.ITSLValueCache;
import com.yanfan.iot.domain.AlertLog;
import com.yanfan.iot.domain.Device;
import com.yanfan.iot.domain.Group;
import com.yanfan.iot.mapper.DeviceMapper;
import com.yanfan.iot.mapper.GroupMapper;
import com.yanfan.iot.model.ThingsModels.ValueItem;
import com.yanfan.iot.model.dashBoard.*;
import com.yanfan.iot.service.DashboardService;
import com.yanfan.iot.service.IAlertLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DashboardServiceImpl implements DashboardService {

    @Resource
    private DeviceMapper deviceMapper;
    @Autowired
    private IAlertLogService alertLogService;
    @Resource
    private ITSLValueCache itslValueCache;
    @Resource
    private GroupMapper groupMapper;

    @Override
    public DeviceCount deviceCount(Device device) {
        DeviceCount deviceCount=deviceMapper.deviceCount(device);
        return deviceCount;
    }

    @Override
    public List<AlertLog> alertLog(Device device) {

        List<AlertLog> list=deviceMapper.alertLog(device);
        return list;
    }

    @Override
    public OnlineRate onlineRate(Device device) {
        OnlineRate onlineRate=deviceMapper.onlineRate(device);
        return onlineRate;
    }

    @Override
    public List<DashBoardDeviceGroup> deviceGroup(Device device) {
        List<Group> groups = groupMapper.selectGroupList(new Group());
        List<DashBoardDeviceGroup> dashBoardDeviceGroups =new ArrayList<>();
        for (Group group : groups) {
           if (group.getIsGateway() == 0 ){
               DashBoardDeviceGroup deviceGroup = new DashBoardDeviceGroup();
               List<DynamicDevices> devices= deviceMapper.DashBoardDeviceGroup(group);
               deviceGroup.setGroupName(group.getGroupName());
               deviceGroup.setList(devices);
               deviceGroup.setActiveIndex(1);
               dashBoardDeviceGroups.add(deviceGroup);
           }
        }
        return dashBoardDeviceGroups;

    }

    @Override
    public DeviceIlluminationRateNum deviceIlluminationRate(Device device) {
        List<Device> list = deviceMapper.selectDeviceLampList(device);
        DeviceIlluminationRateNum deviceIlluminationRateNum = new DeviceIlluminationRateNum();
        if (list != null && !list.isEmpty()){
            deviceIlluminationRateNum.setSum(list.size());
            int lightUpNum=0;
//            int lightDownNum=0;
            for (Device d : list) {
                List<ValueItem> valueItems = itslValueCache.getCacheDeviceStatus(d.getProductId(), d.getSerialNumber());
                if (valueItems !=null && !valueItems.isEmpty()) {
                    for (ValueItem item : valueItems) {
                        if ("sw".equals(item.getId())) {
                            if ("1".equals(item.getValue())) {
                                lightUpNum++;
                                deviceIlluminationRateNum.setOnline(lightUpNum);
                            }
//                            else {
//                                lightDownNum++;
//                                deviceIlluminationRateNum.setUnderline(lightDownNum);
//                            }
                            break;
                        }
                    }
                }
            }
            double result = (double) lightUpNum / list.size();
            DecimalFormat df = new DecimalFormat("#0.00");
            String formattedResult = df.format(result * 100);
            deviceIlluminationRateNum.setUnderline(list.size() -lightUpNum );
            deviceIlluminationRateNum.setRate(formattedResult + "%");
        }
        return deviceIlluminationRateNum;
    }

    @Override
    public Map<String, Object> deviceStatus(Device device) {
        Map<String, Object> map=new HashMap<>();
        List<DeviceStatus> list=deviceMapper.deviceStatusList();
        List<DeviceStatus> deviceStatus=deviceMapper.deviceStatus();
        map.put("deviceStatusList",list);
        map.put("deviceStatus",deviceStatus);
        return  map;
    }

    @Override
    public List<AlertLog> dlqAlertLog(Device device) {
        List<AlertLog> list=deviceMapper.dlqAlertLog(device,3);
        return list;
    }

    @Override
    public List<AlertLog> minorIssues(Device device) {
        List<AlertLog> list=deviceMapper.dlqAlertLog(device,2);
        return list;
    }

    @Override
    public List<String> getAllLight(Device device) {
        List<String> list=deviceMapper.getAllLight();
//        String join = list.stream().collect(Collectors.joining(","));
        return list;
    }

    @Override
    public List<DonitorDeviceGroup> monitorDeviceGroup(Device device) {

      List<DonitorDeviceGroup> deviceGroups=  deviceMapper.donitorDeviceGroup();
        return deviceGroups;
    }

    @Override
    public MonitorOnlineVo monitorOnlineNum(Device device) {
        MonitorOnlineVo deviceStatus = deviceMapper.monitorOnlineNum();
        return deviceStatus;
    }

    @Override
    public List<monitorDistributeData> monitorDistributionProportion(Device device) {

        List<monitorDistributeData> dataList = deviceMapper.monitorDistributeData();
        return dataList;
    }

    @Override
    public Map<String, Object> deviceOverview(Device device) {
        Map<String, Object> map=new HashMap<>();
        DeviceOverview light =deviceMapper.deviceOverviewLight();
        DeviceOverview electric =deviceMapper.deviceOverviewElectric();
        DeviceOverview fire =deviceMapper.deviceOverviewFire();
        DeviceOverview security =deviceMapper.deviceOverviewSecurity();
        map.put("light",light);
        map.put("electric",electric);
        map.put("fire",fire);
        map.put("security",security);
        return  map;
    }

    @Override
    public DeviceOverview getCenterData(Device device) {
        DeviceOverview centerData = deviceMapper.getCenterData();
        return centerData;
    }

    @Override
    public  List<AlertLog> getWarningData(Device device) {
        List<AlertLog> list=deviceMapper.getWarningData();
        return list;
    }

    @Override
    public List<monitorDistributeData> getAreaList(Device device) {
        List<monitorDistributeData> dataList = deviceMapper.getAreaList();
        return dataList;
    }

    @Override
    public List<DeviceUnderlineData> getUnderlineData(Device device) {
        List<DeviceUnderlineData> dataList = deviceMapper.getUnderlineData();
        return dataList;
    }

    @Override
    public List<AlertLog> getDailyAlertCount(Device device) {
        List<AlertLog> dataList = deviceMapper.getDailyAlertCount();
        return dataList;
    }

    @Override
    public List<AlertLog> getTodayAlert(Device device) {
        List<AlertLog> dataList = deviceMapper.getTodayAlert();
        return dataList;
    }

    @Override
    public List<AlertLog> getElectricDailyAlertCount(Device device) {
        List<AlertLog> dataList = deviceMapper.getElectricDailyAlertCount();
        return dataList;
    }

    @Override
    public List<AlertLog> getElectricTodayAlert(Device device) {

        List<AlertLog> dataList = deviceMapper.getElectricTodayAlert();
        return dataList;
    }

    @Override
    public Map<String, Object> getElectricDeviceData(Device device) {
        Map<String, Object> map=new HashMap<>();
        List<ElectricDeviceData> list=deviceMapper.getElectricDeviceData();
        map.put("deviceStatusList",list);
        return  map;
    }

    @Override
    public List<DeviceOnlineRate> getOnlineData(Device device) {
        return deviceMapper.getOnlineData(device);
    }


}
